var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();




function initMap(mapa,numMapa,convId, componenteSeam) {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = mapa;
  
  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
  
  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);
  }
  
  
  var callback1=function(result){
	  document.getElementById("fromInf").innerHTML=result;
  };
  var callback2=function(result){
	  document.getElementById("toInf").innerHTML=result;
  };
  
  	var marker;
  	click1Listen = function(overlay,point) {
		map.clearOverlays();
	    marker = new GMarker(point, {draggable: true});
		map.addOverlay(marker);
		Seam.Remoting.getContext().setConversationId( convId );
		Seam.Component.getInstance(componenteSeam).updateFromPoint(point.lat(),point.lng(),callback1);
		
	};
  	click2Listen = function(overlay,point) {
		map.clearOverlays();
	    marker = new GMarker(point, {draggable: true});
		map.addOverlay(marker);
		Seam.Remoting.getContext().setConversationId( convId );
		Seam.Component.getInstance(componenteSeam).updateToPoint(point.lat(),point.lng(),callback2);
		
	};		
if(numMapa==1){
	GEvent.addListener(map, "click", click1Listen);
}
else if(numMapa==2){
	GEvent.addListener(map, "click", click2Listen);
}
}

function traceRouteInMap(map, fromLat, fromLon, toLat, toLon){
	
	var center = new google.maps.LatLng((fromLat+toLat)/2.0, (fromLon + toLon)/2.0);
	map.setCenter(center);
	var from = new google.maps.LatLng(fromLat, fromLon);
	var to = new google.maps.LatLng(toLat, toLon);
	var marcador =new GMarker(from);
	map.addOverlay(marcador);
	marcador =new GMarker(to);
	map.addOverlay(marcador);
	
	var line = new GPolyline([from, to], "#00eedd", 3, 1); 
	map.addOverlay(line);
}

